#############################################################################################################################################################
### Setup 

# Source setup file
setwd('~/Dropbox/Projects/Dissertation/Bill Text and Agenda Control/JOP Replication Files')
source('text_setup.R')

# Load cosine similarity distribution data
csA <- readRDS(file = 'Data/cosineSimilarityAnalysis_Combined_rr.rds')



###############################################################################################################################################################
### Figure 3

# Open device
tiff(filename='Figures/fg3.tif', width=9, height=4, units='in', res=1200)

# Plotting parameters
par(oma = c(3, 0, 0, 0), mgp = c(3, 0, -1), mar = c(5.1, 4.1, 1.1, 1.1), las=2)
plot(NULL, xlim = c(2, 39), ylim = c(0,0.5), xlab = '', ylab = '', xaxt = 'n', yaxt = 'n', main = '', bty = 'n')
cols <- c('#000000', '#525252', '#969696')

# Loop through topics, plot distributions
for(ii in 1:length(csA)){
  # Subset
  csP <- csA[[ii]]
  
  # Medians
  points(2*ii, csP['fn','50%'],pch = 15, col = cols[1])
  points(2*ii+0.45, csP['pfn','50%'], pch = 17, col = cols[2])
  points(2*ii+0.9, csP['xn','50%'], pch = 16, col = cols[3])
  
  # Middle 50%
  lines(c(2*ii, 2*ii), c(csP['fn', '25%'], csP['fn', '75%']), lwd = 3, col = cols[1])
  lines(c(2*ii+0.45, 2*ii+0.45), c(csP['pfn', '25%'], csP['pfn', '75%']), lwd = 3, col = cols[2])
  lines(c(2*ii+0.9, 2*ii+0.9), c(csP['xn', '25%'], csP['xn', '75%']), lwd = 3, col = cols[3])
  
  # Middle 90%
  lines(c(2*ii, 2*ii), c(csP['fn', '5%'], csP['fn', '95%']), lwd = 1, col = cols[1])
  lines(c(2*ii+0.45, 2*ii+0.45), c(csP['pfn', '5%'], csP['pfn', '95%']), lwd = 1, col = cols[2])
  lines(c(2*ii+0.9, 2*ii+0.9), c(csP['xn', '5%'], csP['xn', '95%']), lwd = 1, col = cols[3])
}
topicNames <- c('Economy', 'Civil Rights', 'Health', 'Agriculture', 
                'Labor', 'Education', 'Environment', 'Energy',
                'Transportation', 'Law and Crime', 'Social Welfare', 
                'Housing', 'Banking and Finance', 'Defense', 
                'Technology', 'Foreign Trade', 
                'International Affairs', 'Government Operations')
axis(side = 1, at = (seq(2, 36, by = 2)+0.45), labels = topicNames, tick = FALSE, las = 2, cex.axis = 0.8)
axis(side = 2, at = c(seq(0, 0.4, by=0.1)), cex.axis = 0.8)
title(ylab = 'Cosine Similarity', line = 1.5, cex.lab = 0.8)
legend(x = 28.5, y = 0.5, legend = c('Floor to Floor', 'Pre-Floor to Pre-Floor', 'Floor to Pre-Floor'), 
       pch = c(15, 17, 16), col = cols, lty = c(1, 1, 1), bty = 'n', cex = 0.8)
par(oma = c(0, 0, 0, 0), mgp = c(3, 1, 0), las=1)


dev.off()